import Mock from 'mockjs';
import { Random } from 'mockjs';

// 设置延时，模拟网络请求
Mock.setup({
  timeout: '200-600'
});

// 生成随机套餐数据
const generatePackages = (count = 20) => {
  const packages = [];
  for (let i = 1; i <= count; i++) {
    packages.push({
      id: i,
      name: `${Random.cword(3, 6)}体检套餐`,
      type: Random.pick(['elderly', 'child', 'female', 'male', 'comprehensive']),
      hospitalArea: Random.pick(['east', 'west', 'south', 'north', 'central']),
      price: Random.float(100, 2000, 2, 2),
      items: Random.pick([
        ['general', 'blood', 'urine'],
        ['biochemistry', 'electrocardiogram', 'ultrasound'],
        ['chestXray', 'ct', 'tcm'],
        ['general', 'blood', 'biochemistry', 'electrocardiogram']
      ]),
      description: Random.cparagraph(1, 3),
      status: Random.boolean(),
      imageUrl: Random.image('200x150', Random.color(), Random.color(), Random.cword(2, 4)),
      createdAt: Random.date('yyyy-MM-dd'),
      updatedAt: Random.date('yyyy-MM-dd')
    });
  }
  return packages;
};

// 生成随机项目数据
const generateItems = (count = 30) => {
  const items = [];
  for (let i = 1; i <= count; i++) {
    items.push({
      id: i,
      name: `${Random.cword(2, 5)}检查`,
      category: Random.pick(['blood', 'urine', 'imaging', 'function', 'special']),
      price: Random.float(20, 500, 2, 2),
      referenceRange: `${Random.float(0, 100, 1, 2)}-${Random.float(100, 200, 1, 2)}`,
      unit: Random.pick(['ml', 'g', 'mmol/L', '次', '项']),
      description: Random.cparagraph(1),
      status: Random.boolean(),
      createdAt: Random.date('yyyy-MM-dd'),
      updatedAt: Random.date('yyyy-MM-dd')
    });
  }
  return items;
};

// 生成随机类型数据
const generateTypes = (count = 10) => {
  const types = [];
  for (let i = 1; i <= count; i++) {
    types.push({
      id: i,
      name: `${Random.cword(2, 4)}套餐类型`,
      description: Random.cparagraph(1),
      status: Random.boolean(),
      createdAt: Random.date('yyyy-MM-dd'),
      updatedAt: Random.date('yyyy-MM-dd')
    });
  }
  return types;
};

// 模拟套餐API
const packages = generatePackages();
Mock.mock(/\/packages/, 'get', (options) => {
  const { page = 1, pageSize = 10, name, type, hospitalArea, status } = JSON.parse(options.body || '{}');
  
  let result = [...packages];
  
  if (name) {
    result = result.filter(item => item.name.includes(name));
  }
  
  if (type) {
    result = result.filter(item => item.type === type);
  }
  
  if (hospitalArea) {
    result = result.filter(item => item.hospitalArea === hospitalArea);
  }
  
  if (status !== undefined) {
    result = result.filter(item => item.status === (status === 'true'));
  }
  
  const total = result.length;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const list = result.slice(start, end);
  
  return {
    code: 200,
    message: 'success',
    data: list,
    total
  };
});

Mock.mock(/\/packages\/\d+/, 'put', (options) => {
  const id = options.url.match(/\/packages\/(\d+)/)[1];
  const data = JSON.parse(options.body);
  const index = packages.findIndex(item => item.id === parseInt(id));
  
  if (index !== -1) {
    packages[index] = { ...packages[index], ...data };
    return { code: 200, message: 'success' };
  } else {
    return { code: 404, message: 'Not Found' };
  }
});

// 模拟项目API
const items = generateItems();
Mock.mock(/\/items/, 'get', (options) => {
  const { page = 1, pageSize = 10, name, category, status } = JSON.parse(options.body || '{}');
  
  let result = [...items];
  
  if (name) {
    result = result.filter(item => item.name.includes(name));
  }
  
  if (category) {
    result = result.filter(item => item.category === category);
  }
  
  if (status !== undefined) {
    result = result.filter(item => item.status === (status === 'true'));
  }
  
  const total = result.length;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const list = result.slice(start, end);
  
  return {
    code: 200,
    message: 'success',
    data: list,
    total
  };
});

// 模拟类型API
const types = generateTypes();
Mock.mock(/\/types/, 'get', (options) => {
  const { page = 1, pageSize = 10, name } = JSON.parse(options.body || '{}');
  
  let result = [...types];
  
  if (name) {
    result = result.filter(item => item.name.includes(name));
  }
  
  const total = result.length;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const list = result.slice(start, end);
  
  return {
    code: 200,
    message: 'success',
    data: list,
    total
  };
});
